package com.project.domain.dto.projectApplyDto;

import lombok.Data;

import javax.validation.constraints.NotEmpty;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;


@Data
public class ApplyHistoryReqDto {
    /*项目名称*/
    private String projectName;
    /*申请人名称*/
    private String applierName;
    /*最小申请经费*/
    private Float minFee;
    /*最大申请经费*/
    private Float maxFee;
    /*申请日期*/
//    private String applyDate;
    private String applyDate1;
    private String applyDate2;
    /*立项状态*/
    private ArrayList<String> lixiangState;
    /*所属单位*/
    private ArrayList<String> unitIds;
    /*项目编号*/
    private String projectCode;
    /*一级学科*/
    private ArrayList<String> level1Subjects;
    /*关键词*/
    private String keyword;
    /*申请状态*/
    private ArrayList<String> applyState;
    private Integer pageSize;
    private Integer pageNum;
    private String orderBy;

//    // 转换后的开始和结束日期
//    private LocalDate startDate;
//    private LocalDate endDate;

    /*登录人角色*/
    private String roleKey;
    /*登录人id*/
    private String userId;
    /*登录人管理的单位id*/
    private ArrayList<String> UnitScope;

    /*
    // setter 方法触发转换
    public void setApplyDate(String applyDate) {
        this.applyDate = applyDate;
        convertDateRange(); // 每次设置 applyDate 时自动转换
    }

    // 转换逻辑
    private void convertDateRange() {
        if (this.applyDate == null || this.applyDate.trim().isEmpty()) {
            this.startDate = null;
            this.endDate = null;
            return;
        }

        String trimmed = this.applyDate.trim();
        try {
            if (trimmed.matches("\\d{4}")) { // 单个年份，如 "2018"
                int year = Integer.parseInt(trimmed);
                this.startDate = LocalDate.of(year, 1, 1); // 年初
                this.endDate = LocalDate.of(year, 12, 31); // 年末
            } else if (trimmed.matches("\\d{4}-")) { // "2014-" 表示 2014 及以后
                int year = Integer.parseInt(trimmed.substring(0, 4));
                this.startDate = LocalDate.of(year, 1, 1); // 年初
                this.endDate = null; // 无结束日期
            } else if (trimmed.matches("-\\d{4}")) { // "-2018" 表示 2018 及以前
                int year = Integer.parseInt(trimmed.substring(1));
                this.startDate = null; // 无开始日期
                this.endDate = LocalDate.of(year, 12, 31); // 年末
            } else if (trimmed.matches("\\d{4}-\\d{4}")) { // "2014-2016" 表示范围
                String[] years = trimmed.split("-");
                int startYear = Integer.parseInt(years[0]);
                int endYear = Integer.parseInt(years[1]);
                if (startYear > endYear) {
                    throw new IllegalArgumentException("开始年份不能大于结束年份");
                }
                this.startDate = LocalDate.of(startYear, 1, 1); // 年初
                this.endDate = LocalDate.of(endYear, 12, 31); // 年末
            } else if (trimmed.matches("\\d{4}-\\d{2}-\\d{2}")) { // 完整日期，如 "2023-05-15"
                this.startDate = LocalDate.parse(trimmed, DateTimeFormatter.ISO_LOCAL_DATE);
                this.endDate = this.startDate; // 单天
            } else if (trimmed.matches("\\d{4}-\\d{2}-\\d{2}-\\d{4}-\\d{2}-\\d{2}")) { // "2023-05-15-2023-06-20"
                String[] dates = trimmed.split("-");
                this.startDate = LocalDate.parse(dates[0] + "-" + dates[1] + "-" + dates[2], DateTimeFormatter.ISO_LOCAL_DATE);
                this.endDate = LocalDate.parse(dates[3] + "-" + dates[4] + "-" + dates[5], DateTimeFormatter.ISO_LOCAL_DATE);
                if (this.startDate.isAfter(this.endDate)) {
                    throw new IllegalArgumentException("开始日期不能晚于结束日期");
                }
            } else {
                throw new IllegalArgumentException("无效的日期格式");
            }
        } catch (DateTimeParseException | NumberFormatException e) {
            throw new IllegalArgumentException("日期或年份必须是有效的格式");
        }
    }

     */
}
